<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>

<div class="row">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading header">
                <h3 class="panel-title" id="title">资产采购登记表</h3>
            </div>

            <!-- BASIC FORM ELEMENTS -->
            <!--===================================================-->
            <form id="form_purchase_input" class="panel-body form-horizontal form-padding">
                <input type="hidden" name="keyId" id="keyId" value="${purchase.id}" />
                <input type="hidden" name="numStatus" id="numStatus" value="${purchase.numStatus}" />
                <input type="hidden" name="curDutyId" id="curDutyId" value=""/>

                <div class="form-group">
                    <label class="col-md-2 control-label">
                            <a href="javascript:void(0);" onclick="fn_departments_dialog_select('n','deptName','deptId')" style="color: #0a6dbd">申请部门</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="deptName" name="deptName" class="form-control" disabled value="${purchase.issueDepartment.name}">
                        <input type="hidden" id="deptId" name="deptId" value="${purchase.issueDepartment.id}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">申请事由<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input type="text" id="applyReason" name="applyReason" class="form-control" placeholder="请输入申请事由"
                               value="${purchase.applyReason}" <c:if test="${purchase.numStatus>0}">disabled</c:if> >
                    </div>
                </div>

                <c:if test="${purchase.numStatus==0||purchase==null}">
                <div class="form-group">
                    <div class="col-md-8" style="text-align: right">
                        <input id="btn-add-row" type="button" value="增加"><span id="spanNumItem" style="display: none">1</span>
                    </div>
                </div>
                </c:if>

                <div class="form-group">
                    <label class="col-md-1 control-label">序号</label>
                    <label class="col-md-3 control-label" style="text-align: center">资产名称<i class="fa fa-asterisk txt-color-red"></i></label>
                    <label class="col-md-1 control-label" style="text-align: center">预算金额<i class="fa fa-asterisk txt-color-red"></i></label>
                    <label class="col-md-1 control-label" style="text-align: center">实际采购金额<i class="fa fa-asterisk txt-color-red"></i></label>
                    <label class="col-md-2 control-label" style="text-align: center">采购日期<i class="fa fa-asterisk txt-color-red"></i></label>
                </div>

                <div id="itemRows">
                <c:if test="${purchase.numStatus==0||purchase==null}">
                    <div class="form-group">
                        <label id="labelXH" class="col-md-1 control-label">1</label>
                        <div class="col-md-3">
                            <input type="text" name="itemName" class="form-control" >
                        </div>
                        <div class="col-md-1">
                            <input type="number" name="itemPlanMoney" class="form-control" >
                        </div>
                        <div class="col-md-1">
                            <input type="number" name="itemFactMoney" class="form-control" disabled >
                        </div>
                        <div class="col-md-2">
                            <div class="input-group date">
                                <input type="text" name="itemDatPurchase" class="form-control" disabled >
                                <span class="input-group-addon"><i class="ti-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                </c:if>
                <c:if test="${purchase.numStatus!=null}">
                    <c:forEach var="item" items="${purchase.purchaseItems}" varStatus="status">
                        <div class="form-group" id="itemRow1">
                            <label class="col-md-1 control-label">${status.index + 1}</label>
                            <div class="col-md-3">
                                <input type="text" name="itemName" class="form-control" value="${item.name}" disabled>
                            </div>
                            <div class="col-md-1">
                                <input type="number" name="itemPlanMoney" class="form-control" value="${item.planMoney}" disabled>
                            </div>
                            <div class="col-md-1">
                                <input type="number" name="itemFactMoney" class="form-control" <c:if test="${purchase.numStatus==0}">disabled</c:if>>
                            </div>
                            <div class="col-md-2">
                                <div class="input-group date">
                                    <input type="text" name="itemDatPurchase" class="form-control" <c:if test="${purchase.numStatus==0}">disabled</c:if>>
                                    <span class="input-group-addon"><i class="ti-calendar"></i></span>
                                </div>
                            </div>
                        </div>
                    </c:forEach>
                </c:if>
                </div>

                <div class="form-group">
                    <label class="col-md-4 control-label">金额合计（元）</label>
                    <div class="col-md-1">
                        <input type="text" id="planMoneySum" name="planMoneySum" class="form-control" disabled value="${purchase.planMoneySum}">
                    </div>
                    <div class="col-md-1">
                        <input type="text" id="factMoneySum" name="factMoneySum" class="form-control" disabled value="${purchase.factMoneySum}">
                    </div>
                    <div class="col-md-1">
                        <input id="btn-calc-total" type="button" value="计算合计">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">其他需要说明情况</label>
                    <div class="col-md-6">
                        <input type="text" id="memo" name="memo" class="form-control" placeholder="请输入其他需要说明情况"
                               value="${purchase.memo}" <c:if test="${purchase.numStatus>0}">disabled</c:if> >
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">提交人</label>
                    <div class="col-md-2">
                        <p class="form-control-static">${purchase.creater.name}</p>
                    </div>
                    <label class="col-md-2 control-label">提交日期</label>
                    <div class="col-md-2">
                        <p class="form-control-static"><fmt:formatDate pattern="yyyy-MM-dd" value="${purchase.dateApply}" /></p>
                    </div>
                </div>

                <div class="panel-footer">
                    <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
                        <i class="fa fa-lg fa-mail-reply"></i> 返回
                    </a>
                    <a class="btn btn-success" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存 </a>
                    <c:if test="${purchase==null || purchase.processState=='Draft'}">
                        <a class="btn btn-warning" id="btn-commit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交 </a>
                    </c:if>
                    <c:if test="${purchase!=null && purchase.processState=='Backed'}">
                        <a class="btn btn-warning" id="btn-recommit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 再提交 </a>
                    </c:if>
                    <c:if test="${purchase.numStatus==1}">
                        <a class="btn btn-warning" id="btn-finish-common" href="javascript:void(0);"><i class="fa fa-check"></i> 采购完成 </a>
                    </c:if>
                </div>
                
            </form>
            <!--===================================================-->
            <!-- END BASIC FORM ELEMENTS -->
            
        </div>
    </div>
</div>

<script>
    var index = "${index}";
    var todo = "${todo}";
    var remind = "${remind}";
    var record = "${record}";
    var draft =  "${draft}";

    function getDefaultDept(data){
        $('#deptName').val(data.department);
        $('#deptId').val(data.departmentId);
    }
    var time1 = setInterval("checkDuty()",1000);
    function checkDuty(){
        var curDutyId = $('#curDutyId').val();
        if(curDutyId != "" && typeof (curDutyId) != "undefined"){
            var vActionUrl = '<%=path%>/com/purchase/getDefaultDept?curDutyId='+curDutyId;
            __ajax_get(vActionUrl,null,getDefaultDept);
            clearInterval(time1);
        }
    }

    $(function(){
        var pdata= {
            keyId:$("#keyId").val(),
            entityName:"com.fz.us.oa.core.entity.com.Purchase"
        };
        multiDuty(pdata);
    });

    // 日期选择
    $('.input-group.date').datepicker({
        format: 'yyyy-mm-dd',
        weekStart: 1,
        autoclose: true,
        todayBtn: 'linked',
        todayHighlight: true,
        language: 'zh-CN'
    });

    // 增加按钮的操作
    $("#btn-add-row").bind("click",function(){
        var rows = $("#itemRows").children();
        var template = rows.first().clone();
        // 修改序号
        var labelXH = parseInt($("#spanNumItem").text()) + 1;
        $("#labelXH",template).text(labelXH);
        $("#spanNumItem").text(labelXH);
        // 清除域值 itemName
        $("input[name='itemName']",template).val("");
        $("input[name='itemPlanMoney']",template).val("");
        $("input[name='itemFactMoney']",template).val("");
        $("input[name='itemDatPurchase']",template).val("");

        var lastRow = rows.last();
        template.insertAfter(lastRow);
    });

    // 触发合计统计
    $("#btn-calc-total").click(function(){
        var numRow = 0;
        var rows = $("#itemRows").children();
        var numStatus = parseInt($("#numStatus").val());

        var fn = "",fn1 = "";
        if(numStatus==0||${purchase==null}){
            numRow = parseInt($("#spanNumItem").text());
            fn = "itemPlanMoney";
            fn1 = "planMoneySum";
        }else if(numStatus==1){
            numRow = rows.length;
            fn = "itemFactMoney";
            fn1 = "factMoneySum";
        }

        var sum = 0.0;
        for (var i=0;i<numRow;i++) {
            var row = rows.eq(i);
            var tmp = $("input[name='"+fn+"']",row).val();
            if (tmp) {
                sum +=  parseFloat(tmp);
            }
        }
        $("input#"+fn1).val(sum);
    });

    // 部门选择框
    function fn_departments_dialog_select(isMulti,nameField,idField) {
        __open_dialog_select('选择部门', '<%=path%>/main/department/dialog?multi='+isMulti, function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 获取 user
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_departments_select(callback);
        });
    }

    //校验
    function infoValidate() {
        var tmp = "";
        tmp = $("input#applyReason").val();
        if(!tmp){
            __toastr_error("请输入申请事由！");
            return false;
        }

        var numRow = parseInt($("#spanNumItem").text());
        var rows = $("#itemRows").children();
        for (var i=0;i<numRow;i++) {
            var row = rows.eq(i);
            tmp = $("input[name='itemName']",row).val();
            if (!tmp) {
                __toastr_error("请输入第"+(i+1)+"行的资产名称！");
                return false;
            }
            tmp = $("input[name='itemPlanMoney']",row).val();
            if (!tmp) {
                __toastr_error("请输入第"+(i+1)+"行的预算金额！");
                return false;
            }
        }

        tmp = $("input#planMoneySum").val();
        if(!tmp || parseFloat(tmp)==0){
            __toastr_error("请点击按钮计算合计！");
            return false;
        }

        return true;
    }

    function infoValidate1() {
        var tmp = "";
        var rows = $("#itemRows").children();
        for (var i=0;i<rows.length;i++) {
            var row = rows.eq(i);
            tmp = $("input[name='itemFactMoney']",row).val();
            if (!tmp) {
                __toastr_error("请输入第"+(i+1)+"行的实际采购金额！");
                return false;
            }
            tmp = $("input[name='itemDatPurchase']",row).val();
            if (!tmp) {
                __toastr_error("请输入第"+(i+1)+"行的采购日期！");
                return false;
            }
        }

        tmp = $("input#factMoneySum").val();
        if(!tmp || parseFloat(tmp)==0){
            __toastr_error("请点击按钮计算合计！");
            return false;
        }

        return true;
    }


    //保存
    $("#btn-save-common").click(
            function(e) {
                __confirm_dialog("提示：","确定保存吗？",
                        function () {
                            __form_save("form_purchase_input", "<%=path%>/com/purchase/save",function(){
                                $("#apply-re-common").trigger("click");
                            });
                        }
                )
            }
    );
    
    //提交
    $("#btn-commit-common,#btn-recommit-common").click(
            function(e) {
                var $valid = infoValidate();
                if(!$valid) return false;

                __confirm_dialog("提示：","提交后填写资料不得修改，请确认是否进行提交？",
                        function () {
                            __form_save("form_purchase_input", "<%=path%>/com/purchase/commit",function(){
                                $("#apply-re-common").trigger("click");
                            });
                        }
                )
            }
    );

    //采购完成
    $("#btn-finish-common").click(
        function(e) {
            var $valid = infoValidate1();
            if(!$valid) return false;

            __confirm_dialog("提示：","提交后填写资料不得修改，请确认是否进行提交？",
                function () {
                    __form_save("form_purchase_input", "<%=path%>/com/purchase/finish",function(){
                        $("#apply-re-common").trigger("click");
                    });
                }
            )
        }
    );

    //返回视图
    $("#apply-re-common").click(function(){

        if(index==1){
            loadURL("menu/dashboard",$('#page-content'));
        }else if(todo==1){
            loadURL("menu/toDo",$('#page-content'));
        }else if(remind==1){
            loadURL("menu/remind",$('#page-content'));
        }else if(record==1){
            loadURL("menu/taskRecord?type=1",$('#page-content'));
        }else if(record==2){
            loadURL("menu/taskRecord?type=2",$('#page-content'));
        }else if(draft==1){
            loadURL("menu/draft",$('#page-content'));
        }else{
            returnToHashUrl()
        }
    });


</script>




